<?php
namespace app\admin\model;
use think\Model;
class Conf extends Model
{
    protected static function init()
    {
        //更新前
        Conf::event('before_update',function ($data){
//            dump($data);die;
//            dump($_FILES);die;
            $k = $data['enname'];
            if($_FILES[$k]['tmp_name']){
                $conf = Conf::where('enname',$k)->find();
//                    dump($arts);
                //先判断之前是否有图片，有就删除
                if($conf['value']){
                    $thumbpath = $_SERVER['DOCUMENT_ROOT'].$conf['value'];
                    if (file_exists($thumbpath)){
                        @unlink($thumbpath);
                    }
                }
                //上传本次的图片，文件名是对应的英文名，也就是$k
                $file = request()->file($k);
                $info = $file->move(ROOT_PATH . 'public' . DS .'static/uploads');
                if ($info){
                    $thumb = '/static/uploads'.'/'.$info->getSaveName();
                    //总数据里相应的字段把路径保存进去
                    $data['value'] = $thumb;
                }
            }
//            foreach ($_FILES as $k=>$v){
////                dump($v);die;
//                if ($v['tmp_name']){
////                    $arts = Conf::find($data->id);
//
//                }
//            }
//            dump($data);die;
        });

    }

}
